package com.mxf.example.exception;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * Created by mxf on 2020/7/29.
 */
@Data
public class ErrorBody {
    /**
     * 响应代码
     */
    private int code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private Object data;

    public ErrorBody() {
    }

    public ErrorBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.msg = errorInfo.getResultMsg();
    }
    /**
     * 成功
     *
     * @return
     */
    public static ErrorBody success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static ErrorBody success(Object data) {
        ErrorBody rb = new ErrorBody();
        rb.setCode(ResultEnum.OK.getResultCode());
        rb.setMsg(ResultEnum.OK.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ErrorBody error(BaseErrorInfoInterface errorInfo) {
        ErrorBody rb = new ErrorBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMsg(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ErrorBody error(int code, String message) {
        ErrorBody rb = new ErrorBody();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }
    /**
     * 失败
     */
    public static ErrorBody error( String message) {
        ErrorBody rb = new ErrorBody();
        rb.setCode(-1);
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
